#pragma once
#include "Mutex.hpp"
#include "Cond.hpp"
#include "Thread.hpp"
#include "Log.hpp"
#include <vector>
#include <queue>
#include <iostream>

namespace ThreadPoolModule
{
    using namespace MutexModule;
    using namespace CondModule;
    using namespace ThreadModule;
    using namespace LogModule;

    template <class T>
    class ThreadPool
    {
        void WakeUpOneThread()
        {
            _cond.Signal();
            // LOG(LogLevel::INFO) << "唤醒一个休眠线程";
        }

        void WakeUpAllThread()
        {
            _cond.Broadcast();
            // LOG(LogLevel::INFO) << "唤醒所有休眠线程";
        }

        ThreadPool()
            : _num(5),
              _isrunning(false),
              _sleepnum(0)
        {
            for (int i = 0; i < _num; i++)
            {
                // 这里插入的时候会实例化thread对象
                _thread.emplace_back(
                    // 这里实例化的对象为静态对象，所需需要捕获this指针，才能够调用内部非静态函数
                    [this]()
                    {
                        HandlerTask(); // 想想vector<string> v  v.push_back,v.emplace 将什么数据传入了进去 ，你传入的是数据类型吗？显然传的是数据嘛！
                                       //  vector<int> v;   v.push(int)????? 错！   v.push(3); 对！
                    });
            }
        }

        void Start()
        {
            if (_isrunning)
                return;
            _isrunning = true;

            for (auto &t : _thread)
            {
                t.Start();
            }
        }

        ThreadPool(const ThreadPool<T> &) = delete;
        ThreadPool<T> &operator=(const ThreadPool<T> &) = delete;

    public:
        static ThreadPool<T> *GetInstStance()
        {
            if (Inst == nullptr)
            {
                LockGuard lockguard(_lock);

                LOG(LogLevel::INFO) << "获取单例";
                if (Inst == nullptr)
                {
                    LOG(LogLevel::INFO) << "首次使用单例";
                    Inst = new ThreadPool();
                    Inst->Start();
                }
            }
            return Inst;
        }

        void Stop()
        {
            if (!_isrunning)
                return;
            _isrunning = false;
            if (_sleepnum > 0)
                WakeUpAllThread();
        }

        bool Equeue(const T &in)
        {
            // 这里插入的时候是没有加锁，有点隐患
            if (_isrunning)
            {
                _task.push(in);
                if (_thread.size() - _sleepnum == 0)
                    WakeUpOneThread();
                return true;
            }
            return false;
        }

        void Join()
        {
            for (auto &thread : _thread)
            {
                thread.Join();
            }
        }

        void HandlerTask()
        {
            char name[128];
            pthread_getname_np(pthread_self(), name, sizeof(name));
            while (true)
            {
                T t;
                {
                    LockGuard lockguard(_mutex);
                    {
                        while (_task.empty() && _isrunning)
                        {
                            LOG(LogLevel::INFO) << name << "睡眠";
                            _sleepnum++;
                            _cond.Wait(_mutex);
                            LOG(LogLevel::INFO) << name << "被唤醒";
                            _sleepnum--;
                        }

                        if (_task.empty() && !_isrunning)
                            break;
                        t = _task.front();
                        _task.pop();
                    }
                }
                t(); // 任务的执行
            }
        }

    private:
        int _num;
        std::vector<Thread> _thread; // 这里仅仅是一个声明，只有当向_thread 插入thread对象(非已有的)时，该对象会自动调用其构造
        std::queue<T> _task;
        Cond _cond;
        Mutex _mutex;
        bool _isrunning;
        int _sleepnum;
        static ThreadPool<T> *Inst;
        static Mutex _lock;
    };

    template <class T>
    ThreadPool<T> *ThreadPool<T>::Inst = nullptr;

    template <class T>
    Mutex ThreadPool<T>::_lock;
}
